/**
 * 设置cookie
 *
 * @static
 * @param {*} name 名称
 * @param {*} value 值
 * @param {*} day 过期天数
 * @param {boolean} [isDomain=false] 是否设置在主域下
 * @param {string} [path='/'] 默认归属路径
 * @memberof Util
 */
const setCookie = (name: string, value: string, day = 0, isDomain = false, path = '/') => {
    let domain = '';
    // 设置cookie到主域下
    if (isDomain) {
        // 是否为ip正则
        const regExpr = /^(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)$/;
        // 为ip时忽略
        if (!regExpr.test(location.hostname)) {
            const host = location.hostname;
            if (host.indexOf('.') !== host.lastIndexOf('.')) {
                domain = ';domain=' + host.substring(host.indexOf('.'), host.length);
            }
        }
    }
    // 当设置的时间等于0时，不设置expires属性，cookie在浏览器关闭后删除
    if (day !== 0) {
        const expires = day * 24 * 60 * 60 * 1000;
        const date = new Date(new Date().getTime() + expires);
        document.cookie = `${name}=${escape(value)};path=${path};expires=${date.toUTCString()}${domain}`;
    }
    else {
        document.cookie = `${name}=${escape(value)};path=${path}${domain}`;
    }
}
/**
 * 清除cookie
 *
 * @static
 * @param {string} cookieName
 * @param {boolean} [isDomain] 是否在主域下
 * @memberof Util
 */
const clearCookie = (cookieName: string, isDomain: boolean) => {
    setCookie(cookieName, '', -1, isDomain);
}
/**
 * 获取cookie
 *
 * @static
 * @param {string} name
 * @return {*}  {*}
 * @memberof Util
 */
const getCookie = (name: string) => {
    const reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)');
    const arr = document.cookie.match(reg);
    if (arr && arr.length > 1) {
        return unescape(arr[2]);
    }
    return null;
}

export { setCookie, clearCookie, getCookie };